package com.songmy.spider.run.javaspider;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.log4j.Logger;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Spider.Status;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.songmy.spider.run.vo.SpiderRule;

/**
 * Hello world!
 *
 */
public class App {
	
	private static final String _CHARSET = "UTF-8";
	
	static Logger log = Logger.getLogger(App.class);
	
	public static void main(String[] args) throws FileNotFoundException {
		System.out.println("Hello World!");
		String fullFileName = null ; 
		if(args!=null){
			fullFileName = args[0];
		}

		JSONObject jObject =  JSONObject.parseObject(jsonValue(fullFileName));
		SpiderRule rule = JSON.parseObject(jObject.toJSONString(), SpiderRule.class);
		
		try {
//			rule.setProxyList(getProxyList());
			Spider s =MySpider.start(rule);
			startMemMonitor(s );
		} catch (Exception e) {
			log.error(e);
		}
		
//		System.out.println(jObject.toJSONString());
		
//		String a ="https://www\\.zhihu\\.com/people/\\[a-zA-z|-\\]+/followees";
//		System.err.println(a);
	}
	
	private static void startMemMonitor(final Spider s) {
		Thread t = new Thread(new Runnable() {
			
			public void run() {
				while (s.getStatus() ==Status.Running) {
					Runtime run = Runtime.getRuntime();
					long endMem = run.totalMemory()-run.freeMemory();
					log.info("memory> total:" + run.totalMemory()/1024/1024 + " M free:" + run.freeMemory()/1024/1024 + "M used:" + endMem/1024/1024+" M " );
					log.info("共采集:"+s.getPageCount());
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
				log.info("采集线程停止,当前内容监控停止");
			}
		});
		
		t.start();
		
	}

	public static List<String[]> getProxyList() {
		List<String[]> proxyList = new ArrayList<String[]>();
		try {

			int HttpResult;
			URL url =new URL("http://www.66ip.cn/nmtq.php?getnum=100&isp=1&anonymoustype=2&start=&ports=&export=&ipaddress=&area=1&proxytype=1&api=66ip"); // 创建URL              
			URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码             
			urlconn.connect();              
			HttpURLConnection httpconn =(HttpURLConnection)urlconn;             
			HttpResult = httpconn.getResponseCode();              
			if(HttpResult != HttpURLConnection.HTTP_OK) // 不等于HTTP_OK说明连接不成功                
				log.error("无法连接到");            
			else             {                  
				int filesize = urlconn.getContentLength(); // 取数据长度                
				InputStreamReader isReader = new  InputStreamReader(urlconn.getInputStream());                   
				BufferedReader reader = new BufferedReader(isReader);                 
				StringBuffer buffer = new StringBuffer();     
				String line = "";
				while ((line = reader.readLine()) != null) {
					String ipAndPort="((1?\\d{1,2}|2[1-5][1-5])\\.){3}(1?\\d{1,2}|2[1-5][1-5]):([1-5]?\\d{1,4}|6[1-5][1-5][1-3][1-5])<br/>"; 
					if(line.matches(ipAndPort)){
						proxyList.add(new String[] { line.split(":")[0], line.split(":")[1].replace("<br/>", "") });
					}
				}
			}
			
			
		} catch (FileNotFoundException e) {
			log.error(e);
		} catch (IOException e) {
			log.error(e);
		}
		return proxyList;
	}
	

	public static String jsonValue(String filename) {

		File file = new File(filename);
		Scanner scanner = null;
		StringBuilder buffer = new StringBuilder();
		try {
			scanner = new Scanner(file, _CHARSET);
			while (scanner.hasNextLine()) {
				buffer.append(scanner.nextLine());
			}

		} catch (FileNotFoundException e) {
			log.error(e);
		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
		return buffer.toString();
	}
}
